<!--
# CIS 2515 FINAL PROJECT
# Author: Malacarne, Nicholas SID: 7770
# Description: This is the definition for the 'SuperHTML' class. This class
# is used to create pages with html (CREDITS TO: 
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
<?php

class SuperHTMLDef {

  //properties
  private $title,
          $thePage;

  function __construct($tTitle = ""){
    //constructor
    $this->setTitle($tTitle);
  }

  function getTitle(){
    return $this->title;
  } // end getTitle

  function setTitle($tTitle){
    $this->title = $tTitle;
  }

  function getPage(){
    return $this->thePage;
  }

  //most basic tags
  function addText($content){
    //given any text
    //adds the text to the page
    $this->thePage .= $content;
  }

  function addHTML($content){
    //given any text (including HTML markup)
    //adds the text to the page
    $this->thePage .= $content;
    $this->thePage .="\n";
  }

  function gAddText($content){
    //given any text (including HTML markup)
    //returns the text
    $temp= $content;
    $temp .= "\n";
    return $temp;
  }

  function buildTop(){
    $temp = <<<HERE
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>$this->title</title>
</head>
<body>
HERE;
    $this->addText($temp);
  }

  function buildBottom(){
    //builds the bottom of a generic web page
    $temp = <<<HERE
</body>
</html>
HERE;
    $this->addText($temp);
  }

  //general tag function
  function tag($tagName, $contents){
    //given any tag, surrounds contents with tag
    //improve so tag can have attributes
    $this->addText($this->gTag($tagName, $contents));
  }

  function gTag($tagName, $contents){
    //given any tag, surrounds contents with tag
    //improve so tag can have attributes
    //returns tag but does not add it to page
    $temp = "<$tagName>\n";
    $temp .= "  " . $contents . "\n";
    $temp .= "</$tagName>\n";
    return $temp;
  }

  //header functions
  function h1($stuff){
    $this->tag("h1", $stuff);
  }

  function h2($stuff){
    $this->tag("h2", $stuff);
  }

  function h3($stuff){
    $this->tag("h3", $stuff);
  }

  function h4($stuff){
    $this->tag("h4", $stuff);
  }

  function h5($stuff){
    $this->tag("h5", $stuff);
  }

  function h6($stuff){
    $this->tag("h6", $stuff);
  }

  function gBuildList($theArray, $type = "ul"){
    //given an array of values, builds a list based on that array
    $temp= "<$type> \n";
    foreach ($theArray as $value){
      $temp .= " <li>$value</li> \n";
    }
    $temp .= "</$type> \n";
    return $temp;
  }

  function buildList($theArray, $type = "ul"){
    $temp = $this->gBuildList($theArray, $type);
    $this->addText($temp);
  }

  function gBuildTable($theArray){
    //given a 2D array, builds an HTML table based on that array
    $table = "<table border = 1> \n";
    foreach ($theArray as $row){
      $table .= "<tr> \n";
      foreach ($row as $cell){
        $table .= "  <td>$cell</td> \n";
      }
      $table .= "</tr> \n";
    }
    $table .= "</table> \n";

    return $table;
  }

  function buildTable($theArray){
    $temp = $this->gBuildTable($theArray);
    $this->addText($temp);
  }


  function startTable($border = "1"){
    $this->thePage .= "<table border = \"$border\">\n";
  }

  function gStartTable($border = "1"){
    $temp = "<table border = \"$border\">\n";

    return $temp;
  }

  function tRowArr ($rowData, $rowType = "td"){
    //expects an array in rowdata, prints a row of th values
    $this->thePage .= "<tr> \n";
    foreach ($rowData as $cell){
      $this->thePage .= "  <$rowType>$cell</$rowType> \n";
    }
    $this->thePage .= "</tr> \n";
  }

  function tRow($rowData, $rowType = "td")
  {
      $this->thePage .= "<tr> \n";
      $this->thePage .= "<$rowType>$rowData</$rowType> \n";
      $this->thePage .= "</tr> \n";
  }

  function tData($data, $align = "left", $span = "1")
  {
      $this->thePage .= "<td align = \"$align\" colspan = \"$span\"> \n";
      $this->thePage .= " $data \n";
      $this->thePage .= "</td> \n";
  }

  function gTRow($rowData, $rowType = "td")
  {
      $temp = "<tr> \n";
      $temp .= "<$rowType>$cell</$rowType> \n";
      $temp .= "</tr> \n";

      return $temp;
  }

  function endTable(){
    $this->thePage .= "</table> \n";
  }

   function gEndTable(){
    $temp = "</table> \n";

    return $temp;
  }

  //form elements
  function gTextbox($type, $name, $value = ""){
    // returns but does not print
    // an input type = text element
    // used if you want to place form elements in a table
    $temp = <<<HERE
<input type = "$type"
       name = "$name"
       value = "$value" />

HERE;
    return $temp;
  }

  function textbox($type, $name, $value = ""){
    $this->addText($this->gTextbox($type, $name, $value));
  }

  function gSubmit($value = "Submit Query"){
    // returns but does not print
    // an input type = submit element
    // used if you want to place form elements in a table
    $temp = <<<HERE
	  <input type = "submit"
           value = "$value" />

HERE;
    return $temp;
  }

  function submit($value = "Submit Query"){
    $this->addText($this->gSubmit($value));
  }

  function gSelect($name, $listVals){
    //given an associative array,
    //prints an HTML select object
    //Each element has the appropriate
    //value and displays the associated name
    $temp = "";
    $temp .= "<select name = \"$name\" >\n";
    foreach ($listVals as $val => $desc){
      $temp .= "  <option value = \"$val\">$desc</option> \n";
    }
    $temp .= "</select> \n";
    return $temp;

  }

  function addOption($value, $name)
  {
      $temp = "<option value =\"".$value."\">";
      $temp .= $name."</option>\n";

      $this->addText($temp);
  }

  function startSelect($name, $size = 1)
  {
      $temp = "<select name= \"$name\" size=".$size.">";

      $this->addHTML($temp);
  }

  function endSelect()
  {
      $this->addHTML("</select>");
  }

  function select($name, $listVals){
    $this->addText($this->gSelect($name, $listVals));
  }

  function formResults(){
    //returns the names and values of all form elements
    //in an HTML table
    $this->startTable();
    foreach ($_REQUEST as $name => $value){
      $this->tRow(array($name,$value));
    }
    $this->endTable();
  }

  function gForm($method, $action, $contents)
  {
	$temp = "<form method = \"".$method."\" action = \"".$action."\">\n";
	$temp .= $contents."\n";
	$temp .= "</form>\n";
	return $temp;
  }

  function form($method, $action, $contents)
  {
	$this->addText($this->gForm($method, $action, $contents));
  }

  function image($contents){
	$this->addText($this->gImage($contents));
  }

  function gImage($contents){
	$temp = "<img src = \"".$contents."\">\n";
	return $temp;
  }

  function link($contents, $name){
	$this->addText($this->gLink($contents, $name));
  }

  function gLink($contents, $name){
	$temp = "<a href = \"".$contents."\">";
	$temp .= $name;
	$temp .= "</a>\n";
	return $temp;
  }

  function checkBox($contents, $name){
	$this->addText($this->gCheckBox($contents, $name));
  }

  function gCheckBox($contents, $name){
	foreach ($contents as $value){
		$temp = "<input type = \"checkbox\" ";
		$temp .= "name = \"".$name."\" ";
		$temp .= "value = \"".$value."\">";
		$temp .= $name;
		$temp .= "</input>\n";

		return $temp;
	}
  }

  function colorize($contents, $color)
  {
      $temp = "<font color = ".$color.">";
      $temp .= $contents;
      $temp .= "</font>";

      $this->addText($contents);
  }

  function flush()
  {
      $this->thePage = "";
  }
  
  function textArea($data, $rows, $col)
  {
      $temp = "<textarea ";
      $temp .= " rows=\"$row\" cols=\"$col\"";
      $temp .= "\n";
      $temp .= $data;
      $temp .= "\n";
      $temp .= "</textarea>";
      
      $this->addText($temp);
  }

  function newLine()
  {
      $this->addText("\n");
  }
}
?>
    </body>
</html>
